package com.qqtech.qquan.user.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.enums.UserIdentityTypeDictEnum;
import com.qqtech.qquan.user.enums.UserVerifycodeTypeDictEnum;
import com.qqtech.qquan.user.model.UserIdentityQuery;
import com.qqtech.qquan.user.model.UserIdentityVo;
import com.qqtech.qquan.user.model.UserVerifycode;
import com.qqtech.qquan.user.model.UserVerifycodeQuery;
import com.qqtech.qquan.user.service.UserIdentityService;
import com.qqtech.qquan.user.service.UserVerifycodeService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/verifycode")
public class UserVerifycodeController extends BaseWebController<UserVerifycode, UserVerifycodeQuery> {
	@Resource
	private UserVerifycodeService userVerifycodeService;
	
	@Resource
	private UserIdentityService userIdentityService;

	@Override
	protected BaseService<UserVerifycode> getBaseService() {
		return userVerifycodeService;
	}

	/**
	 * 使用短信验证码登录时，生成并获取手机验证码
	 * 
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@RequestMapping(value = "/addLoginPhoneCode")
	@ResponseBody
	public Result addLoginPhoneCode(@RequestBody Map<String, String> map) throws BizzException {
		return userVerifycodeService.addVerifycode(UserIdentityTypeDictEnum.PHONE, map.get("phone"),
				UserVerifycodeTypeDictEnum.REG_LOGIN);
	}

	/**
	 * 使用短信验证码更换账号时，生成并获取手机验证码
	 * 
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@RequestMapping(value = "/addChangePhoneCode")
	@ResponseBody
	public Result addChangePhoneCode(@RequestBody Map<String, String> map) throws BizzException {
		return userVerifycodeService.addVerifycode(UserIdentityTypeDictEnum.PHONE, map.get("phone"),
				UserVerifycodeTypeDictEnum.CHANGEIDENTITY);
	}
	
	/**
	 * 使用短信验证码更换密码时，生成并获取手机验证码
	 * 
	 * @param map
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/addChangePasswordCode")
	@ResponseBody
	public Result addChangePasswordCode() throws BizzException {
		Integer curUserId = super.getUserId();
		UserIdentityQuery userIdentityQuery = new UserIdentityQuery();
		userIdentityQuery.setUserId(curUserId);
		userIdentityQuery.setType(UserIdentityTypeDictEnum.PHONE.getCode());
		UserIdentityVo userIdentityVo = userIdentityService.getOne(userIdentityQuery, new PageOrder());
		return userVerifycodeService.addVerifycode(UserIdentityTypeDictEnum.PHONE,userIdentityVo.getIdentity(),
				UserVerifycodeTypeDictEnum.CHANGEPASSWORD);
	}
}
